package cn.tedu.property.management.controller;

import cn.tedu.property.management.pojo.dto.CommunityAddNewDTO;
import cn.tedu.property.management.pojo.dto.CommunityUpdateDTO;
import cn.tedu.property.management.pojo.dto.HouseAddNewDTO;
import cn.tedu.property.management.pojo.dto.HouseUpdateDTO;
import cn.tedu.property.management.pojo.vo.CommunityStandardVO;
import cn.tedu.property.management.pojo.vo.HouseListVO;
import cn.tedu.property.management.pojo.vo.HouseStandardVO;
import cn.tedu.property.management.service.ICommunityService;
import cn.tedu.property.management.service.IHouseService;
import cn.tedu.property.management.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/house")
@Api(tags = "房屋模块")
public class HouseController {
    @Autowired
    private IHouseService houseService;

    @ApiOperation("添加房屋")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(HouseAddNewDTO houseAddNewDTO){
        houseService.addNew(houseAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("根据房号删除房屋")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "unit", value = "单元", required = true, dataType = "String"),
        @ApiImplicitParam(name = "building", value = "楼栋", required = true, dataType = "String"),
        @ApiImplicitParam(name = "floor", value = "楼层", required = true, dataType = "String"),
        @ApiImplicitParam(name = "number", value = "房号", required = true, dataType = "String")
    })
    @PostMapping("/{unit}-{building}-{floor}-{number}/delete")
    public JsonResult<Void> delete(
            @PathVariable String unit,
            @PathVariable String building,
            @PathVariable String floor,
            @PathVariable String number){
        houseService.deleteByNumber(unit, building, floor, number);
        return JsonResult.ok();
    }

    @ApiOperation("修改房屋")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update/{id}")
    public JsonResult updateCommunity(
            @PathVariable Long id, HouseUpdateDTO houseUpdateDTO){
        houseService.update(id,houseUpdateDTO);
        return JsonResult.ok("更新成功！");
    }

    @ApiOperation("根据状态查询房屋数量")
    @ApiOperationSupport(order = 410)
    @GetMapping("/select/{state}")
    public JsonResult countByState(@PathVariable Integer state){
        Integer count = houseService.countByState(state);
        return JsonResult.ok(count);
    }

    @ApiOperation("根据房号查询房屋信息")
    @ApiOperationSupport(order = 420)
    @GetMapping("/{unit}-{building}-{floor}-{number}")
    public JsonResult<HouseStandardVO> getStandardByNumber(
            @PathVariable String unit,
            @PathVariable String building,
            @PathVariable String floor,
            @PathVariable String number){
        HouseStandardVO houseStandardVO = houseService.getStandardByNumber(unit, building, floor, number);
        return JsonResult.ok(houseStandardVO);
    }

    @ApiOperation("根据房屋状态查询房屋集合")
    @ApiOperationSupport(order = 430)
    @GetMapping("/select/list/{state}")
    public JsonResult<List<HouseListVO>> getListByState(@PathVariable Integer state){
        List<HouseListVO> listByState = houseService.getListByState(state);
        return JsonResult.ok(listByState);
    }

    @ApiOperation("根据用户id查询房屋集合")
    @ApiOperationSupport(order = 440)
    @GetMapping("/select/list1/{ownerId}")
    public JsonResult<List<HouseListVO>> getListByOwnerId(@PathVariable Long ownerId){
        List<HouseListVO> listByState = houseService.getListByOwnerId(ownerId);
        return JsonResult.ok(listByState);
    }
}
